<?php
error_reporting(0);
date_default_timezone_set('Europe/London');

require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'conn.ini';

$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("templates/PaymentSchedulesReport.xls");

$data = null;
//echo $_GET['ids'];
$ids = str_replace("-", "," , $_GET['ids']);
//echo $ids;
$sql = "SELECT * FROM cc_paymentschedules WHERE id IN (" . $ids . ")";

$rs = mysql_query($sql);
if(mysql_num_rows($rs)) {
	$i=0;
	while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
		$data[$i]['Paid'] = ($row['paid'] == '1') ? "√" : ' ';
		if($row['paid_date'] != '0000-00-00') {
			$data[$i]['Paiddate'] = date('m/d/Y', strtotime($row['paid_date']));
		}else{
			$data[$i]['Paiddate'] = '';
		}

		if($row['broker_date'] != '0000-00-00') {
			$data[$i]['DueDate'] = date('m/d/Y', strtotime($row['broker_date']));
		}else{
			$data[$i]['DueDate'] = '';
		}
		$data[$i]['USD'] = ($row['exchange'] > 0) ? '$ '. number_format($row['broker_amount'] / $row['exchange'],2): ' ';
		
		$data[$i]['Reference'] = $row['refid_edit'];
		$data[$i]['Payer'] = $row['payment_desc'];
		$data[$i]['Payee'] = $row['payee'];
		$data[$i]['Amount'] = $row['currency'] . ' ' . number_format($row['broker_amount'],2);
		$data[$i]['Exchange'] = $row['exchange'];
		$data[$i]['AmountPaid'] = $row['currency'] . ' ' .number_format($row['paid_amount'],2);
		$data[$i]['Outstanding'] = $row['currency'] . ' ' .number_format($row['broker_amount'] - $row['paid_amount'],2);
		$i++;
	}
}


$baseRow = 12;
if($data != null) {
	$index = 0;
    foreach($data as $r => $dataRow) {
        $row = $baseRow + $r;
        $objPHPExcel->getActiveSheet()->insertNewRowBefore($row,1);
		
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $dataRow['Reference']);
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $dataRow['DueDate']);
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $dataRow['Payer']);
        $objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $dataRow['Payee']);
        $objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $dataRow['Amount']);
        $objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $dataRow['Exchange']);
        $objPHPExcel->getActiveSheet()->setCellValue('H'.$row, $dataRow['USD']);
		$objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $dataRow['Paid']);
		$objPHPExcel->getActiveSheet()->setCellValue('J'.$row, $dataRow['AmountPaid']);
		$objPHPExcel->getActiveSheet()->setCellValue('K'.$row, $dataRow['Paiddate']);
		$objPHPExcel->getActiveSheet()->setCellValue('L'.$row, $dataRow['Outstanding']);
		$objPHPExcel->getActiveSheet()->getRowDimension($row)->setRowHeight(-1);
        $index++;
    }
    $objPHPExcel->getActiveSheet()->removeRow($baseRow-1,1);
	$objPHPExcel->getActiveSheet()->removeRow($baseRow+$index-1,1);
}

mysql_close();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="PaymentSchedulesReport.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
